<template>
  <div class="material-type-manage">
    <el-card shadow="hover">
      <div class="type-header">
        <el-input
          v-model="searchText"
          placeholder="搜索类型名称/编码"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-button type="primary" @click="handleAdd">新增类型</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="code" label="类型编码" width="120px"/>
        <el-table-column prop="name" label="类型名称" width="160px"/>
        <el-table-column prop="sort" label="排序" width="100px" align="center"/>
        <el-table-column prop="remark" label="备注" width="200px"/>
        <el-table-column label="操作" width="180px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="400px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="80px">
        <el-form-item label="类型编码" prop="code">
          <el-input v-model="editRow.code" />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="editRow.name" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="editRow.sort" :min="1" style="width:100%;"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该类型吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 类型定义
interface TypeItem {
  code: string
  name: string
  sort: number
  remark: string
}

// 模拟数据
const typeList = ref<TypeItem[]>([
  { code: 'MAT01', name: '钢筋', sort: 1, remark: '建筑主材' },
  { code: 'MAT02', name: '水泥', sort: 2, remark: '水泥材料' },
  { code: 'MAT03', name: '玻璃', sort: 3, remark: '门窗幕墙' },
  { code: 'MAT04', name: '石膏板', sort: 4, remark: '内隔墙' },
  { code: 'MAT05', name: '铜线', sort: 5, remark: '电气用' },
  { code: 'MAT06', name: '防水涂料', sort: 6, remark: '' }
])

// 搜索
const searchText = ref('')
const filteredList = computed(() => {
  let arr = typeList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.name.includes(searchText.value) ||
        r.code.includes(searchText.value)
    )
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增类型')
const editRow = reactive<TypeItem>({
  code: '',
  name: '',
  sort: 1,
  remark: ''
})
const formRef = ref()
const formRules = {
  code: [{ required: true, message: '必填', trigger: 'blur' }],
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  sort: [{ required: true, message: '必填', trigger: 'blur' }]
}

function handleAdd() {
  dialogTitle.value = '新增类型'
  Object.assign(editRow, { code: '', name: '', sort: typeList.value.length + 1, remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: TypeItem) {
  dialogTitle.value = '编辑类型'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = typeList.value.findIndex(t => t.code === editRow.code)
      if (idx !== -1) typeList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      typeList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<TypeItem | null>(null)
function handleDelete(row: TypeItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    typeList.value = typeList.value.filter(t => t.code !== deleteTarget.value!.code)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.material-type-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.type-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
